$(function () {
    blogData(null, 1, null);
    tweetData();
    friendlyLinkData();
})
var pathname = window.location.pathname;
$("ul.nav-justified>li").each(function () {
    var thisUrl = $(this).children().attr("href");
    if (thisUrl == pathname) {
        $(this).children().addClass("active");
    }
})

/**
 * 用户博客数据列表
 * @param obj
 * @param type
 * @param blogTypeId
 */
function blogData(obj, type, blogTypeId) {
    $("ul.nav-justified>li>a").removeClass('active');
    $("#listDataBody").html("");
    layui.use('flow', function () {

        var flow = layui.flow;
        flow.load({
            elem: '#listDataBody'
            , done: function (page, next) {
                var $ = layui.jquery;
                var lis = [];
                $.post('/blog/list', {"number": page, "type": type, "blogTypeId": blogTypeId}, function (res) {
                    if (res.page) {
                        layui.each(res.page.records, function (index, item) {
                            var name = item.username, abstracts = item.abstracts, banner = item.banner,
                                hot = item.readCount;
                            if (!banner) {
                                banner = item.headimg;
                            }

                            if (item.nickname) {
                                name = item.nickname;
                            }
                            if (item.abstracts) {
                                abstracts = abstracts.substr(0, 46) + '..';
                            }

                            var hotHtml = '';
                            if (hot && hot > 50) {
                                hotHtml = '<div class="mod-angle"><i class="layui-icon" style="font-size: 18px; color: #ED887E">&#xe756;</i> </div>';
                            }
                            lis.push('<div class="mod-b mod-art" >' +
                                hotHtml +
                                '<div class="mod-thumb ">' +
                                '<a class="transition" href="/blog/' + item.blogId + '" target="_blank"><img class="lazy" src="' + banner + '?imageView2/1/w/220/h/165" alt="" ></a>' +
                                '</div>' +
                                '<div class="mob-ctt">' +
                                '<h3><a style="cursor: pointer;" href="/blog/' + item.blogId + '" class="transition msubstr-row2" target="_blank">' + item.title.substr(0, 30) + '</a></h3>' +
                                '<div class="mob-author">' +
                                '<a href="/u/' + item.username + '" target="_blank"><span class="author-name " style="margin-left: 0px;">' + name + '</span></a>' +
                                '<span class="time">' + item.insertDate + '</span>' +
                                '<i class="icon icon-read-count" title="阅读"></i><em>'+item.readCount+'</em>' +
                                '<i class="icon icon-fvr" title="收藏"></i><em>'+item.collectCount+'</em>' +
                                '<i class="icon icon-zan" title="点赞"></i><em>'+item.praiseCount+'</em>' +
                                '</div>' +
                                '<div class="mob-sub">' + abstracts + '</div>' +
                                '</div>' +
                                '</div>'
                            );
                        });

                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }
        });
    });
    if (obj) {
        $(obj).addClass("active");
    } else {
        $("#read").addClass("active");
    }

}

/**
 * 动弹列表
 */
function tweetData() {
    $("#tweetList").html("");
    layui.use('flow', function () {

        var flow = layui.flow;
        flow.load({
            elem: '#tweetList',
            end:" ",
            done: function (page, next) {
                var $ = layui.jquery;
                var lis = [];
                $.post('/tweet/list', null, function (res) {
                    if (res.page) {
                        layui.each(res.page.records, function (index, item) {
                            var name = item.username;
                            if(item.nickname){
                                name = item.nickname;
                            }
                            var active_html ="",active_color="";
                            if(item.currentUserId){
                                active_html = "active",active_color="background-color: #FFFFFF;";
                            }
                            var praiseCount = 0;
                            if(item.praiseCount){
                                praiseCount = item.praiseCount;
                            }
                            lis.push('<li>'+
                                '<div class="story-content">'+
                                '<div class="mt-story-title js-story-title" story-data-show="true">'+
                                '<p class="transition hour-arrow">'+
                                '<span class="icon icon-caret js-mt-index-icon"></span>'+
                                '</p>'+
                                '<ul class="hour-head">'+
                                '<li><a href="/u/'+item.username+'" target="_blank"><img class="hour-tx" src="'+item.headimg+'" alt="头像"></li></a>'+
                                '<li><a href="/u/'+item.username+'" target="_blank"><p>'+name+'</p></a><p>'+item.insertDate+'</p></li>'+
                                '</ul>'+
                                '</div>'+
                                '<div class="mt-index-info-parent">'+
                                '<div class="story-info mt-story-info">'+
                                '<p class="story-detail-hide hour-detail-hide mt-index-cont mt-index-cont2 js-mt-index-cont2">'+
                                item.content+
                                '<a href="/tweet/'+item.id+'" target="_blank" class="mt-index-cont2-a">[&nbsp评论&nbsp]</a>'+
                                '<br><a onclick="tweet_praise('+item.id+')" style="cursor:hand;">' +
                                '<i class="icon icon-like '+active_html+'" style="margin-left: 84%;'+active_color+'" title="点赞"></i><em>&nbsp;' +
                                '<c id="tweetPraiseCount_'+item.id+'">'+praiseCount+'</c></em></a>' +
                                '</p>'+
                                '</div>'+
                                '</div>'+
                                '</div>'+
                                '</li>'
                            );
                        });

                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }

        });
    });
}

/**
 * 友情链接
 */
function friendlyLinkData() {
    $("#friendlyLinkList").html("");
    layui.use('flow', function () {

        var flow = layui.flow;
        flow.load({
            elem: '#friendlyLinkList',
            end:" ",
            done: function (page, next) {
                var $ = layui.jquery;
                var lis = [];
                $.post('/friendly/link/list', null, function (res) {
                    if (res.page) {
                        layui.each(res.page.records, function (index, item) {
                            var desc ='';
                            if(item.desc){
                                desc = '<span class="point">&bull;</span>' +
                                    '<a href="'+item.url+'" target="_blank">'+item.desc.substr(0, 20)+'</a>' ;
                            }
                            lis.push('<li>' +
                                '<div class="mod-thumb">' +
                                '<a href="'+item.url+'" target="_blank">' +
                                '<img src="'+item.pic+'">' +
                                '</a>' +
                                '</div>' +
                                '<div class="project-content">' +
                                '<a href="'+item.url+'" class="c2" target="_blank">'+item.company+'</a>' +
                                desc +
                                '</div>' +
                                '</li>'
                            );
                        });

                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }

        });
    });
}


function tweet_praise(tweetId) {
    if(!logined()){
        showLogin();
        return;
    }
    if($("#tweetPraiseCount_"+tweetId).parent().prev().hasClass("active")){
        tweet_praise_del(tweetId)
    }else{
        tweet_praise_add(tweetId);
    }
}

/**
 * 给该博客点赞
 */
function tweet_praise_add(tweetId) {
    $.ajax({
        url: '/tweet/add/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#tweetPraiseCount_"+tweetId).text();
                $("#tweetPraiseCount_"+tweetId).text(Number(value)+Number(1));
                $("#tweetPraiseCount_"+tweetId).parent().prev().addClass("active").css({"background-color":"#FFFFFF"});
            }
        },error:function (d) {

        }
    });
}

/**
 * 取消点赞
 * @param tweetId
 */
function tweet_praise_del(tweetId) {
    $.ajax({
        url: '/tweet/cut/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#tweetPraiseCount_"+tweetId).text();
                $("#tweetPraiseCount_"+tweetId).text(Number(value)-Number(1));
                $("#tweetPraiseCount_"+tweetId).parent().prev().removeClass("active");
            }
        },error:function (d) {
        }
    });
}
